package libraryPSI;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by User on 19.01.14.
 */
public class BookManager {
    private BookStorage bookStorage;

    public BookManager() {
        bookStorage = new BookStorage();
    }


    //Поиск книги по названию
    public Book getBookByName(String name) {
        List<Book> all = bookStorage.getAllBook();
        Iterator<Book> iter = all.iterator();
        Book tmp;
        while (iter.hasNext()) {
            tmp = iter.next();
            if (tmp.getName().equals(name)) {
                return tmp;
            }
        }
        return null;
    }

    //Поиск книги по автору
    public List<Book> getBooksByAuthor(String author) {

        List<Book> booksByAuthor = new ArrayList<Book>();
        List<Book> iterList = new ArrayList<Book>(bookStorage.getAllBook());
        Iterator<Book> iterAuthor = iterList.iterator();
        while (iterAuthor.hasNext()){
            if(iterList.equals(author)){
                booksByAuthor.add(iterAuthor.next());
            }
        }     return booksByAuthor;
    }

    public Book getBookById(int id) {
        return bookStorage.getBookById(id);
    }

    //добавление новой книги в список newBook
    public void addBook(String name, String author) {
        int newId = bookStorage.getNextIdForBook();
        Book newBook;
        newBook = new Book(newId, name, author);
        bookStorage.addBookToStorage(newBook);

    }


}
